{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SageMaker environment variables\n",
    "SageMaker passes all environment variables - including hyperparameters - to the training scripts as command-line arguments.  Your script can parse these arguments.\n",
    "\n",
    "Arguments include hyperparamers, locations to training/validation datasets, and number of cpus/gpus, etc. \n",
    "\n",
    "A full list of environment variables an be found on the [SageMaker Container](https://github.com/aws/sagemaker-containers#important-environment-variables) GitHub repository.\n",
    "\n",
    "Here is an example of how to parse the arguments in your Python script:\n",
    "\n",
    "```\n",
    "parser = argparse.ArgumentParser()\n",
    "\n",
    "# Hyper-parameters\n",
    "parser.add_argument('--epochs',        type=int,   default=15)\n",
    "parser.add_argument('--learning-rate', type=float, default=0.001)\n",
    "parser.add_argument('--batch-size',    type=int,   default=256)\n",
    "parser.add_argument('--weight-decay',  type=float, default=2e-4)\n",
    "parser.add_argument('--momentum',      type=float, default='0.9')\n",
    "parser.add_argument('--optimizer',     type=str,   default='adam')\n",
    "\n",
    "# SageMaker parameters\n",
    "parser.add_argument('--model_dir',        type=str)\n",
    "parser.add_argument('--model_output_dir', type=str,   default=os.environ['SM_MODEL_DIR'])\n",
    "parser.add_argument('--output_data_dir',  type=str,   default=os.environ['SM_OUTPUT_DATA_DIR'])\n",
    "\n",
    "# Data directories and other options\n",
    "parser.add_argument('--gpu-count',        type=int,   default=os.environ['SM_NUM_GPUS'])\n",
    "parser.add_argument('--train',            type=str,   default=os.environ['SM_CHANNEL_TRAIN'])\n",
    "parser.add_argument('--validation',       type=str,   default=os.environ['SM_CHANNEL_VALIDATION'])\n",
    "parser.add_argument('--eval',             type=str,   default=os.environ['SM_CHANNEL_EVAL'])\n",
    "\n",
    "args = parser.parse_args()\n",
    "```\n",
    "\n",
    "## SageMaker support for Tensorboard\n",
    "SageMaker supports real-time Tensorboard monitoring of your training jobs by storing logs in S3 using a Keras callback as follows:\n",
    "\n",
    "```\n",
    "class Sync2S3(tf.keras.callbacks.Callback):\n",
    "def __init__(self, logdir, s3logdir):\n",
    "    super(Sync2S3, self).__init__()\n",
    "    self.logdir = logdir\n",
    "    self.s3logdir = s3logdir\n",
    "\n",
    "def on_epoch_end(self, batch, logs={}):\n",
    "    os.system('aws s3 sync '+self.logdir+' '+self.s3logdir)\n",
    "    # ' >/dev/null 2>&1'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
